package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopItemListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.AdObject;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.GoodCategoryItemListObjectResponse;
import com.iexbuy.ihg.entity.GoodCategoryObject;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.ShopItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.ui.view.MyGridView;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.iexbuy.ihg.utils.UnitUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class HomeHuiguoFragment extends BaseFragment {
	
	private static HomeHuiguoFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;

	private LinearLayout listHeaderView;
	@ViewInject(R.id.adItemList)
	private RelativeLayout adItemList;
	@ViewInject(R.id.gridCategory)
	private MyGridView gridCategory;
	@ViewInject(R.id.ad0GList)
	private RelativeLayout ad0GList;

	private List<AdObject> adObjects = new ArrayList<AdObject>();
	private List<View> adItemViews = new ArrayList<View>();

	private List<ShopItemListObject> shopItemListObjects = new ArrayList<ShopItemListObject>();
	private int pageNo = 0;
	private ShopItemListAdapter shopItemListAdapter;

	//云购
	private List<GoodCategoryObject> goodCategoryObjects = new ArrayList<GoodCategoryObject>();
	private List<GoodCategoryObject> goodCategoryObjectShows = new ArrayList<GoodCategoryObject>();
	private List<GoodCategoryObject> goodCategoryObjectMores = new ArrayList<GoodCategoryObject>();

	//热门分类
	private List<BusinessType> businessTypes = new ArrayList<BusinessType>();
	private List<View> businessTypePageViews = new ArrayList<View>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_huiguo, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_huo_guo));
		mBaseActivity.setLeftButtonVisibility(View.GONE);
		mBaseActivity.setSlideRightBackEnable(false);

		listHeaderView = (LinearLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_huiguo_header, null);
		ViewUtils.inject(this, listHeaderView);
		listView.addHeaderView(listHeaderView);

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshDate();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});
		msgListRefreshLayout.autoRefresh();
	}

	/**
	 * 跳转页面下啦自动刷新
	 */
	/*public void onResume() {
		super.onResume();
		msgListRefreshLayout.autoRefresh();
	}
*/
	//刷新整个页面数据
	private void refreshDate() {
		pageNo = 0;
		initListDate();
	}

	/**
	 * 查询列表
	 */
	private void initListDate() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("payType", "1");
		paramMap.put("goodsType", "2");
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_DEFAULT_SHOP_PRODUCTS, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}
					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ShopItemListObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopItemListObjectResponse.class);
						if (pageNo == 0) {
							shopItemListObjects.clear();
						}
						if (rsp.getBody() != null) {
							shopItemListObjects.addAll(rsp.getBody());
						}
						initListView();
					}
					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initListView() {
		if (shopItemListAdapter == null) {
			shopItemListAdapter = new ShopItemListAdapter(mBaseActivity, shopItemListObjects);
			listView.setAdapter(shopItemListAdapter);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
					HuanGouItemDetailFragment.setShopItemListObject(shopItemListObjects.get(i - 1));//列表有头部
					CommUtil.openSingleFragmentActivity(mBaseActivity, HuanGouItemDetailFragment.class);
				}
			});
			//加载列表头部
			CommUtil.initAdListData(mBaseActivity, "3", adItemList);
			initHotCategoryData();
			CommUtil.initAdListData(mBaseActivity, "4", ad0GList);
		}
		shopItemListAdapter.notifyDataSetChanged();
	}

	private void loadMore() {
		pageNo ++;
		initListDate();
	}

	/**
	 * 查询热门分类
	 */
	private void initHotCategoryData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_PRODUCT_CATOGERY, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						GoodCategoryItemListObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, GoodCategoryItemListObjectResponse.class);
						goodCategoryObjects.clear();
 						goodCategoryObjects.addAll(rsp.getBody());
						initHotCategoryView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}

	private void initHotCategoryView() {
		Collections.sort(goodCategoryObjects, new Comparator<GoodCategoryObject>() {
			@Override
			public int compare(GoodCategoryObject goodCategoryObject, GoodCategoryObject t1) {
				return goodCategoryObject.getSort().compareTo(t1.getSort());
			}
		});
		goodCategoryObjectShows.add(new GoodCategoryObject("-2", "全部"));
		for (int i=0; i < goodCategoryObjects.size(); i ++) {
			if (i < 6) {
				goodCategoryObjectShows.add(goodCategoryObjects.get(i));
			} else {
				goodCategoryObjectMores.add(goodCategoryObjects.get(i));
			}
		}
		goodCategoryObjectShows.add(new GoodCategoryObject("-1", "..."));
		gridCategory.setAdapter(new BaseAdapter() {
			@Override
			public int getCount() {
				return goodCategoryObjectShows.size();
			}
			@Override
			public Object getItem(int i) {
				return goodCategoryObjectShows.get(i);
			}
			@Override
			public long getItemId(int i) {
				return 0;
			}
			@Override
			public View getView(int i, View view, ViewGroup viewGroup) {
				if (view == null) {
					view = mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.single_text_item, null);
				}
				view.setTag(goodCategoryObjectShows.get(i));
				((TextView) view.findViewById(R.id.name)).setText(goodCategoryObjectShows.get(i).getName());
				if ("-1".equalsIgnoreCase(goodCategoryObjectShows.get(i).getId())) {
					((TextView) view.findViewById(R.id.name)).setTextColor(getResources().getColor(R.color.form_lable));
					//((TextView) view.findViewById(R.id.name)).setTextSize(UnitUtils.dip2px(mBaseActivity, 10));
				}
				return view;
			}

		});
		gridCategory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				HuanGuoSearchFragment.setGoodCategoryObjects(goodCategoryObjects);
				HuanGuoSearchFragment.setSelectGoodCategory(goodCategoryObjectShows.get(i));
				CommUtil.openSingleFragmentActivity(mBaseActivity, HuanGuoSearchFragment.class);
			}
		});
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static HomeHuiguoFragment getHomeFragmentInstent() {
		//if(homeFragmentInstent)
		return homeFragmentInstent;
	}

}
